package com.hongkang.springtrains.repository.config;

import com.hongkang.springtrains.repository.UserRepository;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

@Configuration
@EnableJpaRepositories(basePackageClasses = UserRepository.class)
@PropertySource("classpath:database.properties")//此处classpath:不要省略，防止在web环境下无法发现此文件
public class RepositoryConfig {

    @Autowired
    private Environment environment;

    //创建DataSource
    @Bean(destroyMethod = "close")
    public DataSource createDataSource(){
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setUrl(environment.getProperty("dataSource.url"));
        dataSource.setUsername(environment.getProperty("dataSource.username"));
        dataSource.setPassword(environment.getProperty("dataSource.password"));
        dataSource.setDriverClassName(environment.getProperty("dataSource.driverClassName"));
        return dataSource;
    }

    //创建JPAVendorAdapter
    @Bean
    public JpaVendorAdapter jpaVendorAdapter(DataSource dataSource){
        HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        adapter.setDatabasePlatform("org.hibernate.dialect.PostgreSQLDialect");
        adapter.setDatabase(Database.POSTGRESQL);
        adapter.setPrepareConnection(true);
        adapter.setShowSql(true);

        return adapter;
    }

    //创建EntityManger
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource,JpaVendorAdapter adapter){
        LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setPersistenceUnitName("Repository");
        factoryBean.setPackagesToScan("com.hongkang.springtrains.model");
        factoryBean.setJpaVendorAdapter(adapter);
        return factoryBean;
    }




    /**
     * 创建事务管理器
     */
    @Configuration
    @EnableTransactionManagement
    public static class TransactionConfig{
        @Autowired
        private EntityManagerFactory entityManagerFactory;
        @Bean
        public PlatformTransactionManager transactionManager(){
            JpaTransactionManager transactionManager = new JpaTransactionManager(entityManagerFactory);
            return transactionManager;
        }
    }


}
